package cn.mw.cmdb.processor;

import cn.mw.cmdb.service.ServiceAction;
import cn.mw.microMonitorCommon.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

/**
 * CMDB 业务操作记录
 */
@Slf4j
@Component
public class BusinessOperationService implements ApplicationEventPublisherAware {

    public static final String ID = "businessOperationService";

    private ApplicationEventPublisher publisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }
    public void executeBusinessInstanceOperation(Object destObj, Object ret, ServiceAction action) {
        try {
            Authentication authentication = SecurityUtils.getAuthentication();
            publisher.publishEvent(new BusinessProcessEvent(this, destObj, ret, action, authentication));
        } catch (Exception e) {
            log.error("executeBusinessInstanceOperation failed: ", e);
        }
    }
}
